<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="托盘码" prop="tray_id">
        <el-input
          v-model="queryParams.tray_id"
          placeholder="请输入托盘码"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="库位" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入库位"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--  弹窗  -->
    <el-dialog title="" :visible.sync="show" width="1200px" append-to-body :before-close="close">
      <el-form ref="form" :model="form" label-width="140px" label-position="left">

        <el-divider content-position="center">商品信息</el-divider>

        <el-table :data="form.goodsList" border stripe style="width: 100%;">
          <el-table-column type="expand">
            <template slot-scope="scope">
              <el-form label-position="left" inline class="demo-table-expand">
                <el-form-item label="中文名称">
                  {{ getJson(scope.row.goods_Cname,2) }}
                </el-form-item>
                <el-form-item label="英文名称">
                  {{ getJson(scope.row.goods_Ename,2) }}
                </el-form-item>
                <el-form-item label="制造商">
                  {{ scope.row.manufacturer_name }}
                </el-form-item>
                <el-form-item label="供应商">
                  {{ scope.row.supplier_name }}
                </el-form-item>
                <el-form-item label="规格">
                  {{ getJson(scope.row.specification,1) }}
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column prop="goods_name" label="商品料号">
            <template slot-scope="scope">
              {{ scope.row.goods_name }}
            </template>
          </el-table-column>
          <el-table-column prop="number" label="数量">
            <template slot-scope="scope">
              {{ scope.row.number }}
            </template>
          </el-table-column>
          <el-table-column prop="box_number" label="箱数">
            <template slot-scope="scope">
              {{ scope.row.box_number }}
            </template>
          </el-table-column>

          <el-table-column prop="852963" label="托盘码">
            <template slot-scope="scope">
              {{ scope.row.tray_id }}
            </template>
          </el-table-column>
          <el-table-column prop="barcode" label="状态">
            <template slot-scope="scope">
              <span v-if="scope.row.status == 0">未入库</span>
              <span v-if="scope.row.status == 1">已入库</span>
            </template>
          </el-table-column>
        </el-table>
        <el-row style="top: 30px;margin-bottom: 30px;text-align:center;">
          <el-col :span="24">
            <el-button size="medium" @click="close()">关 闭</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
    <!--  弹窗结束  -->

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          v-action="'tray@create'"-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--        >新增</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          v-if="status != '1'"
          type="primary"
          plain
          icon="el-icon-s-home"
          size="mini"
          :disabled="multiple"
          @click="goStock"
        >一键入库</el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          v-action="'stock@delete'"-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-close"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="cancelKu"-->
      <!--        >取消入库</el-button>-->
      <!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center" width="50" :selectable="selectable" />
      <el-table-column prop="tray_id" label="托盘码" align="center" />
      <el-table-column prop="config_full_name" label="库位" align="center" />
      <el-table-column prop="pallet_no" label="栈板号" align="center" />
      <el-table-column prop="number" label="入库数量" align="center" />
      <el-table-column prop="box_number" label="入库箱数" align="center" />
<!--      <el-table-column prop="LOT1" label="LOT1" align="center" />-->
<!--      <el-table-column prop="LOT2" label="LOT2" align="center" />-->
<!--      <el-table-column prop="LOT3" label="LOT3" align="center" />-->
      <el-table-column prop="status" label="入库状态" align="center">
        <template v-slot="scope">
          <span v-if="scope.row.status === '0'">未入库</span>
          <span v-else-if="scope.row.status === '1'">全部入库</span>
        </template>
      </el-table-column>
      <el-table-column prop="goods_status" label="货物状态" align="center" />
      <el-table-column label="查看货物" align="center">
        <template v-slot="scope">
          <span style="color: #20a0ff;cursor:pointer" @click="see(scope.row)">查看</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--          <el-button-->
          <!--            v-if="scope.row.status === '0'"-->
          <!--            v-action="'tray@update'"-->
          <!--            size="mini"-->
          <!--            type="primary"-->
          <!--            @click="handleEdit(scope.row)"-->
          <!--          >修改-->
          <!--          </el-button>-->
          <el-button
            v-if="scope.row.status === '0'"
            v-action="'tray@delete'"
            size="mini"
            type="danger"
            @click="handleDel(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />
    <!-- 新增/修改 -->
    <AddEdit :open.sync="open" :title="title" :surplus="surplus" :packing_num="packing_num" :warehousing_id="warehousing_id" :warehousing_son_id="warehousing_son_id" :form="form" @getList="getList" />
  </div>
</template>
<script>
import { trayList, trayDel, traySurplus, configDel, goStock } from '@/api/storehouse'
import { stockAdd, stockDel } from '@/api/stock'
import AddEdit from './AddEdit'
export default {
  name: 'StorehouseTray',
  components: {
    AddEdit
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      open: false,
      title: '',
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      form: {},
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        tray_id: undefined,
        name: undefined
      },
      show: false,
      ids: [],
      tray_id: [],
      warehousing_id: '',
      warehousing_son_id: '',
      surplus: 0,
      packing_num: 0,
      status: 0
    }
  },
  watch: {
    '$route.query.page_date': {
      handler(newVal, oldVal) {
        // 判断newVal有没有值监听路由变化
        if (newVal != undefined) {
          // this.getList()
        }
      },
      deep: true,
      immediate: true
    }
  },
  created() {
    this.getList()
    this.status = this.$route.query.status
  },
  methods: {
    /** 查询列表 */
    getList() {
      const that = this
      this.loading = true
      this.queryParams['warehousing_id'] = this.$route.query.warehousing_id
      this.queryParams['warehousing_son_id'] = this.$route.query.warehousing_son_id
      this.warehousing_id = this.$route.query.warehousing_id
      this.warehousing_son_id = this.$route.query.warehousing_son_id
      trayList(this.queryParams).then(response => {
        if (response.code === 10000) {
          that.list = response.data
          that.total = response.count
          that.loading = false
        }
      })
    },
    selectable(row) {
      if (row.status == 0) {
        return true
      }
    },
    /** 查看 */
    see(row) {
      this.show = true
      this.form.goodsList = row.products
    },
    close() {
      this.$emit('update:show', false)
      this.show = false
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.surplusGoods()
      this.form = {}
      this.open = true
      this.title = '添加'
    },
    /** 编辑按钮操作 */
    handleEdit(row) {
      this.surplusGoods()
      this.form = JSON.parse(JSON.stringify(row))
      this.open = true
      this.title = '编辑'
    },
    /** 删除按钮操作 */
    handleDel(row) {
      const that = this
      that.$confirm('是否删除该数据？？？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        configDel({ warehousing_son_id: row.warehousing_son_id, id: row.id }).then(response => {
          that.loading = false
          if (response.code === 10000) {
            that.$message.success(response.message)
            that.getList()
          } else {
            that.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        page: 1,
        limit: 10,
        tray_id: undefined,
        name: undefined
      }
      this.handleQuery()
    },
    /** 选中的项 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.tray_id = selection.map(item => item.tray_id)
      this.multiple = !selection.length
    },
    surplusGoods() {
      // 剩余商品入库数量
      traySurplus({ warehousing_son_id: this.warehousing_son_id }).then(response => {
        if (response.code === 10000) {
          this.surplus = response.data.number
          this.packing_num = response.data.packing_num
        }
      })
    },
    /** 一键入库 */
    enterKu() {
      const that = this
      that.$confirm('确认存入仓库吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        const info = {
          id: that.ids.toString(),
          warehousing_id: that.warehousing_id,
          warehousing_son_id: that.warehousing_son_id
        }
        stockAdd(info).then(response => {
          that.loading = false
          if (response.code === 10000) {
            that.$message.success(response.message)
            that.getList()
          } else {
            that.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    },
    /** 一键入库 */
    goStock() {
      const that = this
      if (that.ids.length == 0) {
        that.$message.error('请勾选要入库的托盘！')
        return false
      }
      that.$confirm('确认存入仓库吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        const info = {
          ids: that.ids,
          warehousing_id: that.warehousing_id,
          warehousing_son_id: that.warehousing_son_id
        }
        goStock(info).then(response => {
          that.loading = false
          if (response.code === 10000) {
            that.$message.success(response.message)
            that.getList()
          } else {
            that.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    },
    /** 取消入库 */
    cancelKu() {
      const that = this
      that.$confirm('确认取消入库吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        const info = {
          id: that.ids.toString(),
          tray_id: that.tray_id.toString(),
          warehousing_id: that.warehousing_id,
          warehousing_son_id: that.warehousing_son_id
        }
        stockDel(info).then(response => {
          that.loading = false
          if (response.code === 10000) {
            that.$message.success(response.message)
            that.getList()
          } else {
            that.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    }
  }
}
</script>

